@TestExecutionListeners(value=TransactionalTestExecutionListener.class) @Transactional public abstract class AbstractTransactionalJUnit4SpringContextTests extends AbstractJUnit4SpringContextTests
Abstract transactional
extension of
AbstractJUnit4SpringContextTests
which adds convenience functionality
for JDBC access. Expects a DataSource
bean and a
PlatformTransactionManager
bean to be defined in the Spring
application context
.
This class exposes a SimpleJdbcTemplate
and provides an easy way to
count the number of rows in a table
,
delete from the database
, and
execute SQL scripts
within a
transaction.
Concrete subclasses must fulfill the same requirements outlined in
AbstractJUnit4SpringContextTests
.
Note: this class serves only as a convenience for extension. If you do not
wish for your test classes to be tied to a Spring-specific class hierarchy,
you may configure your own custom test classes by using
SpringJUnit4ClassRunner
, @ContextConfiguration
, @TestExecutionListeners
, @Transactional
,
etc.
AbstractJUnit4SpringContextTests
,
ContextConfiguration
,
TestExecutionListeners
,
TransactionalTestExecutionListener
,
TransactionConfiguration
,
Transactional
,
NotTransactional
,
Rollback
,
BeforeTransaction
,
AfterTransaction
,
SimpleJdbcTestUtils
,
AbstractTransactionalJUnit38SpringContextTests
,
AbstractTransactionalTestNGSpringContextTests
Modifier and Type | Field and Description |
---|---|
protected SimpleJdbcTemplate |
simpleJdbcTemplate
The SimpleJdbcTemplate that this base class manages, available to subclasses.
|
private java.lang.String |
sqlScriptEncoding |
applicationContext, logger
Constructor and Description |
---|
AbstractTransactionalJUnit4SpringContextTests() |
Modifier and Type | Method and Description |
---|---|
protected int |
countRowsInTable(java.lang.String tableName)
Count the rows in the given table.
|
protected int |
deleteFromTables(java.lang.String... names)
Convenience method for deleting all rows from the specified tables.
|
protected void |
executeSqlScript(java.lang.String sqlResourcePath,
boolean continueOnError)
Execute the given SQL script.
|
void |
setDataSource(javax.sql.DataSource dataSource)
Set the DataSource, typically provided via Dependency Injection.
|
void |
setSqlScriptEncoding(java.lang.String sqlScriptEncoding)
Specify the encoding for SQL scripts, if different from the platform encoding.
|
setApplicationContext
protected SimpleJdbcTemplate simpleJdbcTemplate
private java.lang.String sqlScriptEncoding
public AbstractTransactionalJUnit4SpringContextTests()
@Autowired public void setDataSource(javax.sql.DataSource dataSource)
public void setSqlScriptEncoding(java.lang.String sqlScriptEncoding)
protected int countRowsInTable(java.lang.String tableName)
tableName
- table name to count rows inprotected int deleteFromTables(java.lang.String... names)
names
- the names of the tables from which to deleteprotected void executeSqlScript(java.lang.String sqlResourcePath, boolean continueOnError) throws DataAccessException
The script will normally be loaded by classpath. There should be one statement per line. Any semicolons will be removed. Do not use this method to execute DDL if you expect rollback.
sqlResourcePath
- the Spring resource path for the SQL scriptcontinueOnError
- whether or not to continue without throwing an
exception in the event of an errorDataAccessException
- if there is an error executing a statement
and continueOnError was false